library(randomForest)
## randomForest 4.6-6
## Type rfNews() to see new features/changes/bug fixes.
set.seed(22) # bootstrapで同じ結果を出すために設定
res <- randomForest(Species ~ ., iris, importance = T, proximity = T)
# 変数の重要度(寄与度)の計算
# 決定木を構築する際に該当変数をモデルから除いた際 [1]
# クラス毎の予測精度の低下 [2] 全体での予測精度の低下(Mean Decrease
# Accuracy) [3] Gini indexの減少(Mean Decrease Gini)を出力
importance(res, type = 1)
## MeanDecreaseAccuracy
## Sepal.Length 1.3507
## Sepal.Width 0.8213
## Petal.Length 2.5300
## Petal.Width 2.5454
varImpPlot(res, type = 1) # Mean Decrease Accuracy
importance(res, type = 2)
## MeanDecreaseGini
## Sepal.Length 9.796
## Sepal.Width 2.331
## Petal.Length 42.549
## Petal.Width 44.564
varImpPlot(res, type = 2) # Mean Decrease Gini
# 類似度に基づく階層クラスタリング
ds <- as.dist(1 - res$proximity)
hc <- hclust(ds, "ward")
plot(hc)